﻿using System;

namespace ExpLib.Core
{
    public class ObjectFactory
    {
        /// <summary>
        /// 根据一个长字符串返回此字符串代表的实例 文件夹名“长类名，程序集名”
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T Create<T>(String type)
        {
            return Create<T>(type, true);
        }

        /// <summary>
        /// 根据一个长字符串返回此字符串代表的实例 文件夹名“长类名，程序集名”
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T Create<T>(String type,bool throwExOnFail)
        {
            String assembleName = type.Split(',')[1];
            String className = type.Split(',')[0];
            
            T obj = default(T);
            try
            {

                Activator.CreateInstance(assembleName, className);
            }
            catch
            {
                if (throwExOnFail)
                    throw;
            }

            return obj;
        }
    }
}
